
var user = require("user");
var vote = [3, 5, 7];

cc.Class({
    extends: cc.Component,

    properties: {
        // voteSprite: [cc.SpriteFrame],
        voteBtnNode: [cc.Node],
        votedBtnNode: [cc.Node],

        voteLabel: [cc.Label],
    },

    onLoad: function() {
        var voteState = user.voteState || [];
        voteState[0] = voteState[0] || false;
        voteState[1] = voteState[1] || false;
        voteState[2] = voteState[2] || false;
        user.voteState = voteState;
        this._refresh();
    },

    _refresh: function() {
        for (var i = 0; i < this.voteBtnNode.length; ++i) {
            this.voteBtnNode[i].active = user.voteState[i] == false;
            this.votedBtnNode[i].active = user.voteState[i] == true;
        }

        for (var i = 0; i < this.voteLabel.length; ++i) {
            this.voteLabel[i].string = "支持票数" + vote[i];
        }
    },

    onClickClose: function() {
        this.node.removeFromParent();
    },

    onClickCreate: function() {
        this.node.removeFromParent();
    },

    onClickVote: function(eventid, index) {
        var _index = parseInt(index);
        if (_index < 0 || _index >= 3) return;
        
        user.voteState[_index] = true;
        vote[_index] = vote[_index] + 1;
        this._refresh();
    }
})